package com.urtime.util;

import java.util.HashMap;

import org.json.simple.JSONObject;

public class ErrorJson {
	static final public int ERROR_OK = 0;
	static final public int ERROR_UNKNOWN = 1;
	static final public int ERROR_USER_EXISTS = 2;
	static final public int ERROR_UNKNOWN_METHOD = 3;
	static final public int ERROR_PARAMS = 4;
	static final public int ERROR_USRENAME_PASSWORD = 5;
	static final public int ERROR_NO_LOGIN = 6;
	static final public int ERROR_UNKNOWN_USER = 7;
	static public HashMap<Integer, String> error_info = null;
	static private void build_error_info(){
		error_info = new HashMap();
		error_info.put(ERROR_OK, "OK");
		error_info.put(ERROR_UNKNOWN, "Unknown error.");
		error_info.put(ERROR_USER_EXISTS, "User already exists.");
		error_info.put(ERROR_UNKNOWN_METHOD, "Unknown method.");
		error_info.put(ERROR_PARAMS, "Invalid Params.");
		error_info.put(ERROR_USRENAME_PASSWORD, "Invalid username or password.");
		error_info.put(ERROR_NO_LOGIN, "Login first.");
		error_info.put(ERROR_UNKNOWN_USER, "Unknown user.");
	}
	
	static public String getErrorString(int id){
		if(error_info==null){
			build_error_info();
		}
		String ret = error_info.get(new Integer(id));
		if(ret==null){
			ret = error_info.get(ErrorJson.ERROR_UNKNOWN);
		}
		return ret;
	}
	
	static public JSONObject getErrorJson(int id, String info){
		//if(info==null ) info="null";
		JSONObject obj = new JSONObject();
		obj.put("errid", id);
		obj.put("errtype", getErrorString(id));
		obj.put("errinfo", info);
		return obj;
	}
}
